<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>04员工表格练习</title>
</head>
<body>
    <input type="text" placeholder="请输入员工姓名" id="i1">
    <input type="text" placeholder="请输入员工工资" id="i2">
    <input type="text" placeholder="请输入员工岗位" id="i3">
    <input type="button" value="点我添加员工" onclick="addEmp()">
    <hr>
    <table border="1">
        <caption>员工表</caption>
        <tr>
            <th>姓名</th>
            <th>工资</th>
            <th>岗位</th>
        </tr>
    </table>

<script>
    function addEmp(){
        //1.获取用户输入的值
        let uName = document.getElementById('i1').value;
        let uSalary = document.getElementById('i2').value;
        let uJob = document.getElementById('i3').value;
        //8.判断用户输入的信息是否为空,为空则弹出提示
        if(uName==''||uSalary==''||uJob==''){
            alert('请输入完整信息!');
            return;
        }
        //2.创建表格的tr对象
        let tr = document.createElement('tr');
        //3.创建当前行的3个td对象
        let nameTd = document.createElement('td');
        let salaryTd = document.createElement('td');
        let jobTd = document.createElement('td');
        //4.给对应的td添加文本内容
        nameTd.innerHTML = uName;
        salaryTd.innerHTML = uSalary;
        jobTd.innerHTML = uJob;
        //5.将准备好的td装入tr中
        tr.append(nameTd,salaryTd,jobTd);
        //6.获取table并且将准备好的tr添加到table中
        document.querySelector('table').append(tr);
        //7.插入数据后需要清空输入框
        document.getElementById('i1').value='';
        document.getElementById('i2').value='';
        document.getElementById('i3').value='';
    }
</script>
</body>
</html>